﻿using System;
using System.IO;
using System.Text;
using UnityEngine;

namespace QPEngine
{
    public class Log
    {
        private static readonly StreamWriter info;
        private static readonly StreamWriter error;

        public static bool IsNeedFlush = true;
        private static string dirName = "Logs";
        private static string infoFileName = "/Log-Client-Info.txt";
        private static string errorFileName = "/Log-Client-Error.txt";

        static Log()
        {
            if (RuntimePlatform.WindowsEditor == Application.platform ||
              RuntimePlatform.IPhonePlayer == Application.platform ||
              AppConst.DebugMode)
            {
                return;
            }

            string dirPath = PathHelp.AppHotfixResPath + dirName;

            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
            info = new StreamWriter(dirPath + infoFileName, false, Encoding.Unicode, 1024);
            error = new StreamWriter(dirPath + errorFileName, false, Encoding.Unicode, 1024);
        }

        public static void Warning(string msg)
        {
            DateTime dateTime = DateTime.Now;
            string s = $"{dateTime:yyyy-MM-dd HH:mm:ss} {msg}";

            if (RuntimePlatform.WindowsEditor == Application.platform ||
                RuntimePlatform.IPhonePlayer == Application.platform)
            {
                UnityEngine.Debug.LogWarning(msg);
                return;
            }

            info.WriteLine(s);
            if (IsNeedFlush)
            {
                info.Flush();
            }
        }

        public static void Info(string msg)
        {
            DateTime dateTime = DateTime.Now;
            string s = $"{dateTime:yyyy-MM-dd HH:mm:ss} {msg}";

            if (RuntimePlatform.WindowsEditor == Application.platform ||
                RuntimePlatform.IPhonePlayer == Application.platform ||
                AppConst.DebugMode)
            {
                UnityEngine.Debug.Log(msg);
                return;
            }
            info.WriteLine(s);
            if (IsNeedFlush)
            {
                info.Flush();
            }
        }

        public static void Error(string msg)
        {
            DateTime dateTime = DateTime.Now;
            string s = $"{dateTime:yyyy-MM-dd HH:mm:ss} {TimeHelper.ClientNow()} {msg}";

            if (RuntimePlatform.WindowsEditor == Application.platform ||
                RuntimePlatform.IPhonePlayer == Application.platform ||
                AppConst.DebugMode)
            {
                UnityEngine.Debug.LogError(msg);
                return;
            }

            error.WriteLine(s);
            if (IsNeedFlush)
            {
                error.Flush();
            }
            info.WriteLine(s);
            if (IsNeedFlush)
            {
                info.Flush();
            }
        }

        public static void Debug(string msg)
        {
            DateTime dateTime = DateTime.Now;
            string s = $"{dateTime:yyyy-MM-dd HH:mm:ss} {TimeHelper.ClientNow()} {msg}";

            if (RuntimePlatform.WindowsEditor == Application.platform ||
                RuntimePlatform.IPhonePlayer == Application.platform ||
                AppConst.DebugMode)
            {
                UnityEngine.Debug.Log(msg);
                return;
            }
            info.WriteLine(s);
            if (IsNeedFlush)
            {
                info.Flush();
            }
        }

        public static void Flush()
        {

            if (RuntimePlatform.WindowsEditor == Application.platform ||
                RuntimePlatform.IPhonePlayer == Application.platform ||
                AppConst.DebugMode)
            {
                return;
            }
            info.Flush();
            error.Flush();
        }

        public static void Close()
        {
            if (RuntimePlatform.WindowsEditor == Application.platform ||
                RuntimePlatform.IPhonePlayer == Application.platform ||
                AppConst.DebugMode)
            {
                return;
            }
            info.Close();
            error.Close();
            info.Dispose();
            error.Dispose();
        }
    }
}